// api
import { checkNickname, checkPhone, checkEmail } from '@/api/verify';
import { getVerifyCodeAPI } from '@/api/utils';


export async function verifyCheckNickname({ nickname }) {
    const regexpNickname = /^[a-zA-Z0-9_\u4e00-\u9fa5]{1,32}$/;
    if (nickname.value.trim() === '') {
        nickname.tip = '';
        return;
    }
    if (!regexpNickname.test(nickname.value)) {
        nickname.tip = '昵称格式不正确';
        nickname.state = false;
        return;
    }
    try {
        const { code, message } = await checkNickname({ nickname: nickname.value });
        nickname.tip = message;
        nickname.state = code === 200;
    } catch (error) {
        console.error('校验用户昵称失败');
    }
}

export async function verifyCheckSign({ sign }) {
    const regexPhone = /^[1]([3-9])[0-9]{9}$/;
    const regexEmail = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/;
    if (regexPhone.test(sign.value)) {
        sign.tip = '';
        sign.state = true;
        sign.signType = 'phone'; // 这个就表示可获得验证码的类型
        return;
    }
    if (regexEmail.test(sign.value)) {
        sign.tip = '';
        sign.state = true;
        sign.signType = 'email'; // 这个就表示可获得验证码的类型
        return;
    }
    sign.tip = '';
    sign.state = false;
    sign.signType = null;
}

async function verifySign({ sign }) {
    const funObj = {
        phone: checkPhone,
        email: checkEmail,
    };
    const { signType } = sign;
    const options = {};
    options[signType] = sign.value;
    const { code, message } = await funObj[signType](options);
    sign.tip = message;
    sign.state = code === 200;
    return code === 200;
}

export async function getVerifycode(target) {
    const { nowAction, sign, verifyCode } = target;
    verifyCode.sending = true;
    if (nowAction === 'register' && !(await verifySign(target))) {
    // 在注册操作下,先验证帐号是否可用
        verifyCode.sending = false;
        return;
    }
    const { signType } = sign;
    const options = {};
    options[signType] = sign.value;
    const { code, message, data } = await getVerifyCodeAPI({
        type: signType,
        email: sign.value,
        phone: sign.value,
    });
    verifyCode.tip = message;
    verifyCode.sending = false;
    verifyCode.state = code === 200;
    try {
        verifyCode.expiresIn = data.expiresIn;
    } catch (error) {
        verifyCode.expiresIn = data;
    }
    // 这里expiresIn是肯定会有一个时间戳的
    const timer = setInterval(() => {
        const verifyCodeCD = Math.ceil((verifyCode.expiresIn - Date.now()) / 1000);
        if (verifyCodeCD > 0) {
            verifyCode.tip = `验证码的CD还有: ${verifyCodeCD}s`;
            verifyCode.disabled = true;
        } else {
            verifyCode.tip = '';
            verifyCode.disabled = false;
            clearInterval(timer);
        }
    }, 1000);
}

export async function verifyCheckPassword(target) {
    const { password } = target;
    const regexPassword = /^\w{6,16}$/;
    if (!regexPassword.test(password.value)) {
        password.tip = '密码格式为6-16个字符组成，区分大小写';
        password.state = false;
    } else {
        password.tip = '';
        password.state = true;
    }
}
